<?php
/*
 * @Author       : chaihao
 * @Date         : 2020-06-04 14:20:31
 * @FilePath     : /ai/controllers/api/SendMessageController.php
 */

namespace app\controllers\api;

use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\controllers\api\ApiController;
use app\Services\api\SendMessageService;
use Yii;
use yii\web\Controller;

class SendMessageController extends Controller
{
    public $req = array();
    public $user = array();

    /**
     * 安全 校验 屏蔽
     * 预处理  权限等等操作处理
     */
    public function beforeAction($action)
    {
        $this->req = Helper::in(array_merge($_POST, $_GET));
        // $this->user = Helper::checksign($this->req);
        // unset($this->req['token']);
        return true;
    }
    /**
     * 发送验证码
     */
    // public function actionSend()
    // {
    //     $req = $this->req;
    //     if (!isset($req['username']) || empty($req['username'])) {
    //         Helper::right_out($req, ['coed' => -1, 'message' => 'username 不能为空']);
    //     }
    //     $rules = array(
    //         'username'  => "required",
    //         'type' => 'required|integer'
    //     );
    //     # 请求参数验证
    //     if (is_numeric($req['username'])) {
    //         Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
    //         $type = 'mobile';
    //     } else {
    //         $rules['username'] = "required|email";
    //         Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
    //         $type = 'email';
    //     }
    //     $res = SendMessageService::getInstance()->send($req, $type);
    //     Helper::right_out($req, $res);
    // }


    /**
     * 测试邮件发送
     */
    public function actionSendEmail()
    {
        // $mail = Yii::$app->mailer->compose();
        // // $mail->setTo('1327503612@qq.com'); //要发送给那个人的邮箱 
        // $mail->setTo('chaihao0823@gmail.com'); //要发送给那个人的邮箱 
        // $mail->setSubject("邮件主题"); //邮件主题 
        // $mail->setTextBody('测试text'); //发布纯文字文本 
        // // $mail->setHtmlBody("测试html text"); //发送的消息内容 
        // var_dump($mail->send());
        $req = $this->req;
        $data =  SendMessageService::getInstance()->sendEmail($req['toEmail'], $req['message_subject'], $req['content']);
        return $data;
    }

    /**
     * 测试 sms 短信发送
     */
    public function actionSendSms()
    {
        $req = $this->req;
        $res = SendMessageService::getInstance()->send();
        // print_r($data);
        Helper::right_out($req, $res);
    }


    /**
     * 添加消息发送记录
     *
     */
    public function actionInsertLog()
    {
        $req = $this->req;
        $res = SendMessageService::getInstance()->insertLog($req);
        // print_r($data);
        Helper::right_out($req, $res);
    }
    /**
     * 修改消息发送状态
     *
     */
    public function actionUpdateLogStatus()
    {
        $req = $this->req;
        $res = SendMessageService::getInstance()->updateLogStatus($req);
        // print_r($data);
        Helper::right_out($req, $res);
    }

    public function actionGetSendNum()
    {
        $req = $this->req;
        $rules = array(
            'send_to'  => "required",
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $res = SendMessageService::getInstance()->getSendNum($req['send_to']);
        // print_r($data);
        Helper::right_out($req, $res);
    }
}
